package com.sfh.cp.user.pst;

import com.sfh.cp.AppContext;
import com.sfh.cp.user.ReqLogin;
import com.sfh.lib.AppCacheManager;
import com.sfh.lib.RxBusEventManager;
import com.sfh.lib.mvp.service.AbstractPresenter;

import java.util.List;
import java.util.Map;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import crm.sfh.server.Const;
import crm.sfh.server.SUser;
import crm.sfh.server.entity.EUser;


/**
 * 功能描述:
 *
 * @date 2018/5/1
 */
public class PstLogin extends AbstractPresenter<IPstLogin.IVLogin> implements IPstLogin {


    @Override
    public void login(ReqLogin req) {

        getView().showLoading(true);
        BmobQuery<SUser> bmobQuery = new BmobQuery<>();
        bmobQuery.addWhereEqualTo("phone",req.getPhone());
        bmobQuery.addWhereEqualTo("password",req.getPassword());
        bmobQuery.findObjects(AppCacheManager.getApplication(), new FindListener<SUser>() {
            @Override
            public void onSuccess(List<SUser> list) {
                getView().hideLoading();
                if (list == null || list.isEmpty()){
                    getView().showToast("账号或密码不正确");
                    return;
                }
                EUser eUser = list.get(0).getUser();
                RxBusEventManager.postEvent(eUser);
                AppCacheManager.newInstance().putCache(true, Const.USER,list.get(0));
                getView().onLoginSuccess();
            }

            @Override
            public void onError(int i, String s) {
                getView().hideLoading();
                getView().showToast(s);
            }
        });
    }

}
